#include <reg52.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "timer.h"
#include "uart.h"
#include "gpio.h"
#include "seg4bitdynamic.h"
#include "HX711.h"
//#include "SSD1315.H"


typedef unsigned char BYTE;
typedef unsigned int WORD;
#define uchar unsigned char
#define uint  unsigned int

unsigned long hxdd;
bit FlagTest=0;

void GetWeight(void)
{
	unsigned char str[10];
	unsigned int number;
	int i=0,weight;
	int nWeight;
	unsigned long ADCTemp;
	if(FlagTest==1)
	{
		ADCTemp=HX711_Read();		
		if(ADCTemp!=0)
		{		
				FlagTest=0;
				printf("DIG=%lu\n",ADCTemp);		//往电脑发送采集到的数字量（自己观察并写公式得到与重量相关的值）
        }				
	}
}

void main(void)
{	
	int i=0;
	unsigned long ADCTemp;
	Delay_ms(2000);
	Timer0_Init();
	Uart1Init();
	EA=1;
	P2=0XFF;	
	SEG_DispInit();UartSendString("Boot Success!\r\n");
	Buzzer_Beep(4);//响4声
	Get_WeightValue(888);SEG7_Buffer[0]=11;//显示-888
	while(1)
	{
		GetWeight(); //称重
	  SEG_DynDisplay();//数码管显示
	}
}


/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4
{
    if (RI)
    {
        RI = 0;             //Clear receive interrupt flag
    }
    if (TI)
    {
        TI = 0;             //Clear transmit interrupt flag
        busy = 0;           //Clear transmit busy flag
    }
} 

/*----------------------------
Timer0 interrupt service routine
----------------------------*/
void Timer0_ISR (void) interrupt 1 using 0
{
	static uchar Counter;	 
	TL0 = (65536-Timer0_DT);     
	TH0 = (65536-Timer0_DT)>>8;     //定时器赋予初值
	Counter ++;
	if (Counter >=30)
	{		 
			FlagTest = 1;		
			Counter=0;		
	}
	keyscan(); 		 		
}